package com.cs.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.cs.controller.utils.R;
import com.cs.pojo.Administrator;
import com.cs.pojo.User;
import com.cs.service.IUserService;
import com.cs.service.admin.Impl.IAdministratorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.HtmlUtils;

import java.io.IOException;

@Slf4j
@RestController
@RequestMapping("/admin")
public class AdminController_R {
    @Autowired
    private IAdministratorService iAdministratorService;

    @GetMapping()
    public R getAll() throws IOException {
//        if (true) throw new IOException();
        return new R("200",iAdministratorService.list());
    }

    @PostMapping
    public R save(@RequestBody Administrator administrator){
        return new R(iAdministratorService.save(administrator));
    }
    @PutMapping
    public R update(@RequestBody Administrator administrator){
        return new R(iAdministratorService.updateById(administrator));
    }
    @DeleteMapping("{id}")
    public R delete(@PathVariable Integer id){
        return new R(iAdministratorService.removeById(id));

    }

    @PostMapping("/login")
    public R login(@RequestBody Administrator administrator){
        System.out.println(administrator.toString());
        // 对 html 标签进行转义，防止 XSS 攻击
        String username=administrator.getUsername();
        username= HtmlUtils.htmlEscape(username);
        String password=administrator.getPassword();
//        QueryChainWrapper<Administrator> username = iAdministratorService.query().eq("username", administrator.getUsername());
//        QueryChainWrapper<Administrator> password = iAdministratorService.query().eq("password", administrator.getUsername());
//        System.out.println(username.toString());
//        System.out.println(password.toString());
        QueryWrapper<Administrator> queryWrapper = new QueryWrapper<Administrator>();
        queryWrapper.eq("username",username).eq("password",password);
        Administrator ad = iAdministratorService.getOne(queryWrapper);
        if(ad!=null){
            return new R("200",true,"登录成功");
        }else {
            return new R("400",false,"用户名或密码错误");
        }
    }

    /*@GetMapping("/{currentPage}/{pageSize}")
    public R getPage(@PathVariable int currentPage,@PathVariable int pageSize){
        IPage<User> page = iUserService.getPage(currentPage, pageSize);
        //如果当前页码值大于总页码值，重新执行查询操作，使用最大页码值作为当前页
        if (currentPage>page.getPages()){
            page=iUserService.getPage((int) page.getPages(),pageSize);
        }
        return new R(true,page);
    }*/

    @GetMapping("/{currentPage}/{pageSize}")
    public R getPage(@PathVariable int currentPage,@PathVariable int pageSize,Administrator administrator){
        IPage<Administrator> page = iAdministratorService.getPage(currentPage, pageSize,administrator);
        //如果当前页码值大于总页码值，重新执行查询操作，使用最大页码值作为当前页
        if (currentPage>page.getPages()){
            page=iAdministratorService.getPage((int) page.getPages(),pageSize,administrator);
        }
        return new R("200",page);
    }
}
